import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '@/stores/modules/auth';
import { ElMessage } from 'element-plus';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      redirect: '/dashboard',
      component: () => import('@/views/Home.vue'),
      children: [
        { // 仪表盘
          path: '/dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard/DashboardView.vue')
        },
        { // 用户管理
          path: '/user',
          name: 'User',
          component: () => import('@/views/User/UserView.vue')
        },
        { // 设备管理
          path: '/device',
          redirect: '/device/list',
          children: [
            { path: '/device/list', name: 'DeviceList', component: () => import('@/views/Device/DeviceList.vue') }, // 设备列表
            { path: '/device/monitor', name: 'DeviceMonitor', component: () => import('@/views/Device/DeviceMonitor.vue') } // 设备详情
          ]
        },
        { // 故障表
          path: '/fault',
          redirect: '/fault/report',
          children: [
            { path: '/fault/report', name: 'FaultList', component: () => import('@/views/Fault/FaultReport.vue') }, // 故障列表
            { path: '/fault/process', name: 'FaultProcess', component: () => import('@/views/Fault/FaultProcess.vue') } // 故障处理
          ]
        },
        { // 维护计划管理
          path: '/maintenance',
          name: 'Maintenance',
          children: [
            { path: '/maintenance/plan', name: 'MaintenancePlan', component: () => import('@/views/Maintenance/Plan.vue') }, // 维护计划列表
            { path: '/maintenance/task', name: 'MaintenanceTask', component: () => import('@/views/Maintenance/Task.vue') } // 维护任务
          ]
        },
        { // 登录注册
          path: '/auth',
          redirect: '/auth/login',
          children: [
            { path: '/auth/login', name: 'Login', component: () => import('@/views/Auth/Login.vue') }, // 登录
            { path: '/auth/register', name: 'Register', component: () => import('@/views/Auth/Register.vue') } // 注册
          ]
        }
      ]
    },
    { path: '/login', name: 'Login', component: () => import('@/views/Auth/Login.vue') }, // 登录
    { path: '/register', name: 'Register', component: () => import('@/views/Auth/Register.vue') } // 注册
  ]
});

// 定义不同角色不可访问的路径
const roleRestrictedUrls = {
  '0': [], // 系统管理员
  '1': ['/fault/process', '/fault/report','/maintenance/plan', '/maintenance/task'], // 设备管理人员
  '2': ['/device/list', '/device/monitor', '/fault/process', '/fault/report'], // 维修人员
  '3': ['/device/list', '/device/monitor','/maintenance/plan', '/maintenance/task'], // 普通用户
};

router.beforeEach((to, from, next) => {
  // 获取用户信息
  const user = JSON.parse(localStorage.getItem('user'));
  const authStore = useAuthStore();
  if (user && user.data) {
    // 更新Pinia store中的用户信息
    authStore.$patch((state) => {
      state.id = user.data.id;
      state.name = user.data.name;
      state.role = user.data.role; // 提取角色信息
    });
    // 获取用户角色
    const userRole = user.data.role;
    // 检查用户角色是否有权限访问该路径
    const restrictedPaths = roleRestrictedUrls[userRole] || [];
    if (restrictedPaths.includes(to.path)) {
      next('/'); // 用户没有权限访问该路径，重定向到首页
      ElMessage.error('您没有权限访问该页面');
    } else {
      next(); // 用户有权限访问该路径
    }
  } else {
    // 如果用户未登录，检查是否为公开路径
    const publicPaths = ['/login', '/register','/dashboard'];
    if (publicPaths.includes(to.path)) {
      next(); // 公开路径，允许访问
    } else {
      next('/login'); // 需要登录的路径，重定向到登录页面
    }
  }
});

export default router;
